﻿using System;
using System.Collections.Generic;
using System.Text;
using Business.Entities;
using System.Data;
using System.Data.SqlClient;

namespace Data.Database
{
    public class PlanAdapter : Adapter
    {
        public List<Plan> GetAll()
        {

            List<Plan> planes = new List<Plan>();

            try
            {
                this.OpenConnection();
                SqlCommand cmdPlanes = new SqlCommand("select * from planes", SqlConn);
                SqlDataReader drPlanes = cmdPlanes.ExecuteReader();


                while (drPlanes.Read())
                {
                    Plan pl = new Plan();
                    pl.ID = (int)drPlanes["id_Plan"];
                    pl.Descripcion = (string)drPlanes["desc_plan"];
                    pl.Idespecialidad = (int)drPlanes["id_especialidad"];

                    planes.Add(pl);

                }
                drPlanes.Close();
            }
            catch (Exception Ex)
            {
                Exception ExcepcionManejada =
                new Exception("Error al recuperar lista de planes", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

            return planes;
        }

        public Business.Entities.Plan GetOne(int ID)
        {
            Plan pl = new Plan();

            try
            {
                this.OpenConnection();
                SqlCommand cmdPlanes = new SqlCommand("select  * from planes where id_Plan = @id", SqlConn);
                cmdPlanes.Parameters.Add("@id", SqlDbType.Int).Value = ID;
                SqlDataReader drPlanes = cmdPlanes.ExecuteReader();
                if (drPlanes.Read())
                {

                    pl.ID = (int)drPlanes["id_Plan"];
                    pl.Descripcion = (string)drPlanes["desc_plan"];
                    pl.Idespecialidad = (int)drPlanes["id_especialidad"];

                }
                drPlanes.Close();
            }
            catch (Exception Ex)
            {
                Exception ExcepcionManejada =
                new Exception("Error al recuperar datos del Plan", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

            return pl;
        }



        public void Delete(int ID)
        {

            try
            {
                this.OpenConnection();
                SqlCommand cmdDelete = new SqlCommand("delete from planes where id_Plan=@id", SqlConn);
                cmdDelete.Parameters.Add("@id", SqlDbType.Int).Value = ID;
                cmdDelete.ExecuteNonQuery();
            }

            catch (Exception Ex)
            {
                Exception ExcepcionManejada = new Exception("Error al eliminar Plan", Ex);
                throw ExcepcionManejada;

            }

            finally
            {
                this.CloseConnection();
            }


        }


        protected void Update(Plan Plan)
        {
            try
            {
                this.OpenConnection();
                SqlCommand cmdSave = new SqlCommand(
                "UPDATE planes set desc_plan=@desc_plan, id_especialidad=@id_especialidad WHERE id_Plan=@id", SqlConn);
                cmdSave.Parameters.Add("@id", SqlDbType.Int).Value = Plan.ID;
                cmdSave.Parameters.Add("@desc_plan", SqlDbType.VarChar).Value = Plan.Descripcion;
                cmdSave.Parameters.Add("@id_especialidad", SqlDbType.Int).Value = Plan.Idespecialidad;
                cmdSave.ExecuteNonQuery();
            }

            catch (Exception Ex)
            {
                Exception ExcepcionManejada = new Exception("Error al modificar datos del Plan", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

        }


        protected void Insert(Plan Plan)
        {
            try
            {
                this.OpenConnection();
                SqlCommand cmdSave = new SqlCommand(
                "insert into planes(desc_plan,id_especialidad) " +
                "values(@desc_plan, @id_especialidad) " +
                "select @@identity", SqlConn);

                cmdSave.Parameters.Add("@desc_plan", SqlDbType.VarChar).Value = Plan.Descripcion;
                cmdSave.Parameters.Add("@id_especialidad", SqlDbType.Int).Value = Plan.Idespecialidad;
                Plan.ID = Decimal.ToInt32((decimal)cmdSave.ExecuteScalar());
            }

            catch (Exception Ex)
            {
                Exception ExcepcionManejada = new Exception("Error al crear Plan", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

        }





        public void Save(Plan Plan)
        {
            if (Plan.State == BussinessEntity.States.Deleted)
            {
                this.Delete(Plan.ID);
            }
            else if (Plan.State == BussinessEntity.States.New)
            {
                this.Insert(Plan);
            }
            else if (Plan.State == BussinessEntity.States.Modified)
            {
                this.Update(Plan);
            }
            Plan.State = BussinessEntity.States.Unmodified;

        }


        public List<Plan> GetAllPlanesEspecialidad(int IDEspecialidad)
        {

            List<Plan> planes = new List<Plan>();

            try
            {
                this.OpenConnection();
                SqlCommand cmdPlanes = new SqlCommand("select * from planes where id_especialidad=@id_especialidad", SqlConn);
                cmdPlanes.Parameters.Add("@id_especialidad", SqlDbType.Int).Value = IDEspecialidad;

                SqlDataReader drPlanes = cmdPlanes.ExecuteReader();


                while (drPlanes.Read())
                {
                    Plan pl = new Plan();
                    pl.Descripcion = (string)drPlanes["desc_plan"];

                    planes.Add(pl);

                }
                drPlanes.Close();
            }
            catch (Exception Ex)
            {
                Exception ExcepcionManejada =
                new Exception("Error al recuperar lista de planes", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

            return planes;
        }


        public Business.Entities.Plan BuscaPlan(string desc)
        {
            Plan pl = new Plan();

            try
            {
                this.OpenConnection();
                SqlCommand cmdPlanes = new SqlCommand("select  * from planes where desc_Plan = @desc", SqlConn);
                cmdPlanes.Parameters.Add("@desc", SqlDbType.VarChar,50).Value = desc;
                SqlDataReader drPlanes = cmdPlanes.ExecuteReader();
                if (drPlanes.Read())
                {

                    pl.ID = (int)drPlanes["id_Plan"];
                    pl.Descripcion = (string)drPlanes["desc_plan"];
                    pl.Idespecialidad = (int)drPlanes["id_especialidad"];

                }
                drPlanes.Close();
            }
            catch (Exception Ex)
            {
                Exception ExcepcionManejada =
                new Exception("Error al recuperar datos del Plan", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

            return pl;
        }


    }
}
